<?php
/**
 * Created by PhpStorm.
 * User: vanity
 * Date: 2018/1/2
 * Time: 14:26
 */

namespace Admin\Controller;
use Think\Controller;
Class CommonController extends Controller
{
    public function _initialize()
    {
        $admin = session('admin');
        if(empty($admin)){
            redirect(U('Admin/login/index'));
        }
        if(in_array(strtolower(ACTION_NAME),array('index','edit','create','delete')) && strtolower(CONTROLLER_NAME) != 'index'){
            if(!$this->checkRole()){
                $this->error('权限不足',U('admin/index/index'));
            }
        }
        $this->assignMsg();
        $this->assign('admin',$admin);
        $this->assign('menu',$this->getMenu());
    }
    public function assignMsg(){
        $success = session('success');
        $error = session('error');
        if(!empty($success)){
            session('success',null);
            $this->assign('success',$success);
        }
        if(!empty($error)){
            session('error',null);
            $this->assign('error',$error);
        }
    }
    public function checkRole()
    {
        $path_arr = explode('/',trim(strtolower(__SELF__),'/'));
        if (isset($path_arr[1])) {
            $urlPath = $path_arr[0] . '.' . $path_arr[1] . '.index';
        } else {
            $urlPath = $path_arr[0] . '.index';
        }
        return $this->check($urlPath);
    }
    public function check($urlPath){
        $id = session('admin.id');
        if($id == 1){
            return true;
        }
        $permissionPath = D('role_user')->getPermissionPath($id);
        if($permissionPath == 'all permission'){
            return true;
        }
        return in_array($urlPath,$permissionPath);
    }
    /*
     * $error_msg  错误信息 array
     * $url  返回地址
     * **/
    public function withError($error_msg,$url)
    {
        $error = $error_msg;
        if(is_array($error_msg)){
            $error = '';
            foreach($error_msg as $key => $value){
                $error .= $key.'|'.$value.'<br/>';
            }
        }
        session('error',$error);
        redirect($url);
    }
    /*
     * $error_msg  错误信息 array
     * $url  返回地址
     * **/
    public function withSuccess($success_msg,$url)
    {
        session('success',$success_msg);
        redirect($url);
    }

    /**
     * 获取左边菜单栏
     * @return array
     */
    public function getMenu()
    {
        $openArr = [];
        $data = [];
        $data['top'] = [];
        //查找并拼接出地址的别名值
        $path_arr = explode('/',trim(strtolower(__SELF__),'/'));
        if (isset($path_arr[1])) {
            $urlPath = $path_arr[0] . '.' . $path_arr[1] . '.index';
        } else {
            $urlPath = $path_arr[0] . '.index';
        }
        //查找出所有的地址
        $table = D('Permissions')->where("name LIKE '%index' OR cid = 0")->select();
        foreach ($table as $v) {
            if ($v['cid'] == 0 || $this->check($v['name'])) {
                if ($v['name'] == $urlPath) {
                    $openArr[] = $v['id'];
                    $openArr[] = $v['cid'];
                }
                $data[$v['cid']][] = $v;
            }
        }

        foreach ($data[0] as $v) {
            if (isset($data[$v['id']]) && is_array($data[$v['id']]) && count($data[$v['id']]) > 0) {
                $data['top'][] = $v;
            }
        }
        unset($data[0]);
        //ation open 可以在函数中计算给他
        $data['openarr'] = array_unique($openArr);
        return $data;
    }
    /*
     *
     * 文件上传类
     * **/
    public function upload(){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
        $upload->savePath  =     '/portrait/'; // 设置附件上传（子）目录
        // 上传文件
        $info   =   $upload->upload();
        if(!$info) {// 上传错误提示错误信息
            $result = [];
            $result['status'] = 0;
            $result['msg'] = $upload->getError();
            return $result;
        }else{// 上传成功
            $info['status'] = 1;
            return $info;
        }
    }
}